﻿@model IEnumerable<ContentCategory>
@{
    TempData["subMenu-check"] = new string[] { "CMS", "ManageContentCategories" };
    ViewData["Title"] = new string[] { "栏目管理" };
}
<div class="row tn-page-heading tn-mb-10">
    <div class="col-xs-12">
        <ol class="breadcrumb">
            <li><a href="@SiteUrls.Instance().ManageCMS()">资讯管理</a></li>
            <li class="tn-active">栏目管理</li>
        </ol>
    </div>
</div>
<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="form-group">
                <a class="btn btn-default layercategorycreat"><i class="fa fa-plus"></i> 新建栏目</a>
            </div>
        </div>
    </div>
</div>

@if (Model == null)
{
    <div class="row text-sm wrapper">
        没有查询到数据 @Model.Count();
    </div>
}
else
{
    var count = Model.Count();
    <div class="panel panel-default">
        <div class="panel-body">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>栏目名称</th>
                        <th>栏目ID</th>
                        <th>内容模型</th>
                        <th>内容数</th>
                        <th>是否启用</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @for (var i = 0; i < count; i++)
                    {
                        var folder = Model.ElementAt(i);
                        <tr class="@folder.ParentId-@folder.Depth" name="@folder.ParentId-@folder.Depth" id="@folder.CategoryId">
                            <td>
                                @if (folder.Depth > 0)
                                {
                                    for (var j = 0; j < folder.Depth; j++)
                                    {
                                    <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                    }
                                    if (i + 1 < count)
                                    {
                                        if (folder.Depth > Model.ElementAt(i + 1).Depth)
                                        {
                                        <span>&nbsp;&nbsp;&nbsp;&nbsp;└─</span>
                                        }
                                        else
                                        {
                                        <span>&nbsp;&nbsp;&nbsp;&nbsp;├─</span>
                                        }
                                    }
                                    else
                                    {
                                    <span>&nbsp;&nbsp;&nbsp;&nbsp;└─</span>
                                    }
                                }
                                &nbsp;<b title="@folder.CategoryName">@HtmlUtility.TrimHtml(folder.CategoryName, 30)</b>
                            </td>
                            <td>
                                @folder.CategoryId
                            </td>
                            <td>
                                @string.Join("、", folder.ContentTypes.Select(n => n.ModelName))
                            </td>
                            <td>@folder.ContentCategoryCount()</td>
                            <td>
                                <a href="javascript:;" role="button" title="是否启用">
                                    <i class="fa fa-check fa-@(folder.IsEnabled ? "check" : "ban") tn-@(folder.IsEnabled ? "green-color" : "red-color")"></i>
                                </a>
                            </td>
                            <td>
                                @if (folder.IsEnabled)
                                {  <a class="layercategorycreatchildren" data-parentid="@folder.CategoryId" role="button" title="添加子栏目"><i class="fa fa-plus  fa-lg"></i></a><text>&nbsp;</text>
                                }
                                <a class="layercategoryupdata" data-parentid="@folder.CategoryId" role="button" title="编辑栏目"><i class="fa fa-pencil  fa-lg"></i></a>&nbsp;

                                <a id="delete-@folder.CategoryId" role="button" title="删除栏目"><i class="fa fa-trash fa-lg"></i></a>&nbsp;
                                @if (i > 0 && folder.Depth <= Model.ElementAt(i - 1).Depth)
                                {
                                <a href="javascript:;" role="button" title="上移"><i class="fa fa-arrow-up fa-lg"></i></a><text>&nbsp;</text>
                                }
                                @if (Model.Where(n => n.ParentId == folder.ParentId && n.Depth == folder.Depth && folder.DisplayOrder < n.DisplayOrder).Count() > 0)
                                {
                                <a href="javascript:;" role="button" title="下移"><i class="fa fa-arrow-down fa-lg"></i></a>
                                }
                            </td>
                        </tr>
                    }

                </tbody>
            </table>
        </div>
    </div>
}

@Styles.Render("~/Bundle/Styles/zTree")


<script type="text/javascript">
    require(['jquery', 'tnlayer', 'ztree', 'userSelector'], function ($, tnlayer) {
        $(".layercategorycreat").click(function () {
            tnlayer.layermodal('添加栏目', '@SiteUrls.Instance()._EditContentCategories()?t=' + new Date(), '650px', '650px')
        })
        $(".layercategorycreatchildren").click(function () {
            var parentId = $(this).data("parentid");
            tnlayer.layermodal('添加栏目', '@CachedUrlHelper.Action("_EditContentCategories", "ControlPanel", "ConsoleViews")?parentId=' + parentId + '&t=' + new Date(), '650px', '640px')
        })
        $(".layercategoryupdata").click(function () {
            var parentId = $(this).data("parentid");
            tnlayer.layermodal('编辑栏目', '@CachedUrlHelper.Action("_EditContentCategories", "ControlPanel", "ConsoleViews")?categoryId=' + parentId + '&t=' + new Date(), '650px', '670px')
        })

        //点击上移按钮
        $(".fa-arrow-up").click(function (e) {
            e.preventDefault();
            var tr = $(this).closest('tr');
            var name = tr.attr("name");
            var fromContentCategoryId = tr.attr("id");
            var toContentCategoryId;
            var trs = $('.' + name);

            for (var i = 0; i < trs.length; i++) {
                if ($(trs[i]).attr("id") === fromContentCategoryId) {
                    toContentCategoryId = $(trs[i - 1]).attr("id");
                    break;
                }
            }
            var parm = jQuery.param({ fromContentCategoryId: fromContentCategoryId, toContentCategoryId: toContentCategoryId, t: new Date().getTime() }, true);
            $.post('@SiteUrls.Instance().ChangeContentCategories()', parm, function (data) {
                if (data.MessageType) {
                    layer.msg(data.MessageContent, {
                        icon: 1
                    });
                    window.location.reload();
                }
                else {
                    layer.msg(data.MessageContent, {
                        icon: 2
                    });
                }
            });

        });

        //点击下移按钮
        $(".fa-arrow-down").click(function (e) {
            e.preventDefault();
            var tr = $(this).closest('tr');
            var name = tr.attr("name");
            var fromContentCategoryId = tr.attr("id");
            var toContentCategoryId;
            var trs = $('.' + name);
            for (var i = 0; i < trs.length; i++) {
                if ($(trs[i]).attr("id") == fromContentCategoryId) {
                    toContentCategoryId = $(trs[i + 1]).attr("id");
                    break;
                }
            }
            var parm = jQuery.param({ fromContentCategoryId: fromContentCategoryId, toContentCategoryId: toContentCategoryId, t: new Date().getTime() }, true);
            $.post('@CachedUrlHelper.Action("ChangeContentCategories", "ControlPanel")', parm, function (data) {
                if (data.MessageType) {
                    layer.msg(data.MessageContent, {
                        icon: 1
                    });
                    window.location.reload();
                }
                else {
                    layer.msg(data.MessageContent, {
                        icon: 2
                    });
                }
            });
        });

        //删除
        $(document).on('click', 'a[id^=delete-]', function (e) {
            var id = $(this).attr("id").replace(/[^0-9]/ig, "");
            layer.confirm("确定删除么？", {
                btn: ['确认', '取消'],//按钮
                scrollbar: false
            }, function () {
                var parm = jQuery.param({ categoryId: id, t: new Date().getTime() }, true);
                $.post('@CachedUrlHelper.Action("DeleteContentCategories", "ControlPanel")', parm, function (data) {
                    if (data.MessageType) {
                        layer.msg(data.MessageContent, {
                            icon: 1
                        });
                        window.location.reload();
                    }
                    else {
                        layer.msg(data.MessageContent, {
                            icon: 2
                        });
                    }
                });
            });
        });

    })
</script>